package com.nami.store.biz.groupmembs.repository.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nami.store.biz.groupmembs.repository.GroupMemberRepository;
import com.nami.store.domain.GroupMemberDO;
import com.nami.store.mapper.GroupMemberMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 群成员 服务实现类
 * </p>
 *
 * @author lbc
 * @since 2023-08-18
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class GroupMemberServiceImpl implements GroupMemberRepository {

    private final GroupMemberMapper baseMapper;

    @Override
    public List<GroupMemberDO> getGrpMembers(String groupId) {
        if (StringUtils.isBlank(groupId)) {
            return null;
        }
        List<GroupMemberDO> groupMembers = baseMapper.selectList(new LambdaQueryWrapper<GroupMemberDO>().eq(GroupMemberDO::getGroupId, groupId));
        return groupMembers;
    }

    @Override
    public GroupMemberDO getGrpMember(String groupId, String userId) {
        if (StringUtils.isBlank(groupId) || StringUtils.isBlank(userId)) {
            return null;
        }
        return baseMapper.selectOne(new LambdaQueryWrapper<GroupMemberDO>().eq(GroupMemberDO::getGroupId, groupId).eq(GroupMemberDO::getUserId, userId));
    }

    @Override
    public boolean batchSave(List<GroupMemberDO> groupMembers) {
        if (CollUtil.isEmpty(groupMembers)) {
            return false;
        }
        List<BatchResult> results = baseMapper.insert(groupMembers);
        // 校验是否完成todo
        return Boolean.TRUE;
    }

    @Override
    public List<GroupMemberDO> getGrpMembers(String groupId, List<String> userIds) {
        if (StringUtils.isBlank(groupId) || CollUtil.isEmpty(userIds)) {
            return null;
        }
        List<GroupMemberDO> result = baseMapper.selectList(new LambdaQueryWrapper<GroupMemberDO>()
            .eq(GroupMemberDO::getGroupId, groupId).in(GroupMemberDO::getUserId, userIds));
        return result;
    }
}
